//
//  SpecificTrainingViewModel.m
//  JiZhuiAn
//
//  Created by Easy.Lin on 16/6/29.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "SpecificTrainingViewModel.h"
#import "APIService+Training.h"
#import "TrainingProjectData.h"
#import "Node.h"

@implementation SpecificTrainingViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    
}

- (LoadingRACCommand *)getTrainingProjectCommand
{
    if (!_getTrainingProjectCommand) {
        _getTrainingProjectCommand = [[LoadingRACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            return [[[[APIService shareService] getTrainingProjectWithPart:self.trainingPart severity:self.severity] doNext:^(NSArray * x) {
                NSMutableArray * dataArray = [NSMutableArray array];
                NSMutableArray * nodeArray = [NSMutableArray array];
                for (BmobObject * each in x) {
                    TrainingProjectData * data = [[TrainingProjectData alloc] initFromBmobObject:each];
                    [dataArray addObject:data];
                }
                if (dataArray.count%3 == 0) {
                    for (int index = 0; index < dataArray.count; index ++) {
                        Node * node;
                        static int nodeId;
                        if (index%3 == 0) {
                            nodeId = index/3 + 1;
                            node = [[Node alloc] initWithParentId:-1
                                                           nodeId:nodeId
                                                             name:[NSString stringWithFormat:@"第%zd天",index/3 + 1]
                                                            depth:0
                                                           expand:YES
                                                             data:nil];
                            [nodeArray addObject:node];
                        }
                        TrainingProjectData * indexData = dataArray[index];
                        node = [[Node alloc] initWithParentId:nodeId
                                                       nodeId:index+30
                                                         name:[NSString stringWithFormat:@"%@ %@",indexData.time,indexData.projectDescription]
                                                        depth:1
                                                       expand:NO
                                                         data:dataArray[index]];
                        [nodeArray addObject:node];
                    }
                }
                else if (dataArray.count%2 == 0) {
                    for (int index = 0; index < dataArray.count; index ++) {
                        Node * node;
                        static int nodeId;
                        if (index%2 == 0) {
                            nodeId = index/2 + 1;
                            node = [[Node alloc] initWithParentId:-1
                                                           nodeId:nodeId
                                                             name:[NSString stringWithFormat:@"第%zd天",nodeId]
                                                            depth:0
                                                           expand:YES
                                                             data:nil];
                            [nodeArray addObject:node];
                        }
                        TrainingProjectData * indexData = dataArray[index];
                        node = [[Node alloc] initWithParentId:nodeId
                                                       nodeId:index+20
                                                         name:[NSString stringWithFormat:@"%@ %@",indexData.time,indexData.projectDescription]
                                                        depth:1
                                                       expand:NO
                                                         data:dataArray[index]];
                        [nodeArray addObject:node];
                    }
                }
                else {
                    for (int index = 0; index < dataArray.count; index ++) {
                        Node * dateNode;
                        Node * dataNode;
                        dateNode = [[Node alloc] initWithParentId:-1
                                                       nodeId:index + 1
                                                         name:[NSString stringWithFormat:@"第%zd天",index + 1]
                                                        depth:0
                                                       expand:YES
                                                             data:nil];
                        TrainingProjectData * indexData = dataArray[index];
                        dataNode = [[Node alloc] initWithParentId:index + 1
                                                           nodeId:index + 10
                                                             name:[NSString stringWithFormat:@"%@ %@",indexData.time,indexData.projectDescription]
                                                            depth:1
                                                           expand:NO
                                                             data:dataArray[index]];
                        
                        [nodeArray addObject:dateNode];
                        [nodeArray addObject:dataNode];
                    }
                }
                self.dataSource = nodeArray;
            }] doError:^(NSError *error) {
                
            }];
        }];
    }
    return _getTrainingProjectCommand;
}

@end
